/**
 * @file 09-maps.ts
 *
 * 1.3.11 使用 Promise 和 async/await 创建异步代码
 *
 */

// Promise only version of the asynchronous code.

// function ExpensiveWebCall(time: number): Promise<void> {
//     return new Promise((resolve, reject) => setTimeout(resolve, time));
// }


// class MyWebService {
//     CallExpensiveWebOperation(): void {
//         ExpensiveWebCall(4000).then(() => console.log(`Finished web service`))
//             .catch(() => console.log(`Expensive web call failure`));
//     }
// }


function ExpensiveWebCall(time: number) {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, time);
        throw new Error("TEst");
    });
}

class MyWebService {
    async CallExpensiveWebOperation() {
        try {
            await ExpensiveWebCall(4000);
            console.log(`Finished web service222`)
        }
        catch (error) {
            console.log(`Caught ${error}`)
        }

    }
}


console.log(`calling service`);
new MyWebService().CallExpensiveWebOperation();
console.log(`Processing continues until the web service returns`);